package bjx.com.kehu.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONException;
import bjx.com.kehu.R;
import bjx.com.kehu.adapter.CardTicketVoucherLvAdapter;
import bjx.com.kehu.contants.Constants;
import bjx.com.kehu.interfaces.ItemMultClickListener;
import bjx.com.kehu.model.CardTicketJsonModel;
import bjx.com.kehu.model.CardTicketModel;
import bjx.com.kehu.model.UserModel;
import bjx.com.kehu.utils.GsonUtil;
import bjx.com.kehu.utils.LogsUtil;
import bjx.com.kehu.utils.SPUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.refreshlayout.BGAMoocStyleRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by jqc on 2017/10/25.
 */

public class CardTicketVoucherListActivity extends BaseActivity {

    ImageView mIVBack;
    TextView mTVTitle;
    TextView mTVEvent;

    BGARefreshLayout mRefreshLayout;
    ListView mListView;
    CardTicketVoucherLvAdapter mAdapter;
    RelativeLayout mRLDel;
    CheckBox mCBDelAll;
    TextView mTVDel;

    List<CardTicketModel.ResultBean.LdataBean> mDriverNearList
            = new ArrayList<CardTicketModel.ResultBean.LdataBean>();

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_nearby_sj_list);
        mIVBack = getViewById(R.id.back);

        mTVTitle = getViewById(R.id.title);
        mTVTitle.setText("我的卡包");
        mTVEvent = getViewById(R.id.event);

        mRLDel = getViewById(R.id.myorder_rl_del);
        mCBDelAll = getViewById(R.id.myorder_cb_delall);
        mTVDel = getViewById(R.id.myorder_tv_del);

        mRefreshLayout = getViewById(R.id.rl_myorder_refresh);
        mListView = getViewById(R.id.myorder_lv);

        BGAMoocStyleRefreshViewHolder moocStyleRefreshViewHolder = new BGAMoocStyleRefreshViewHolder(mApp, true);
        moocStyleRefreshViewHolder.setUltimateColor(R.color.red);
        moocStyleRefreshViewHolder.setOriginalImage(R.mipmap.custom_mooc_icon);
        moocStyleRefreshViewHolder.setRefreshViewBackgroundColorRes(R.color.grey_light);
        moocStyleRefreshViewHolder.setLoadMoreBackgroundColorRes(R.color.grey_light);
        moocStyleRefreshViewHolder.setSpringDistanceScale(0);
        mRefreshLayout.setRefreshViewHolder(moocStyleRefreshViewHolder);
        mRefreshLayout.setTag(false);

        initAdapter();
        loadNearbySJListList();
    }

    private void initAdapter() {
        mAdapter = new CardTicketVoucherLvAdapter(mApp, mDriverNearList);
        mListView.setAdapter(mAdapter);

        mAdapter.setItemClickListener(new ItemMultClickListener() {
            @Override
            public void onItemClick(View view, int postion) {

                CardTicketModel.ResultBean bean =
                        (CardTicketModel.ResultBean) mAdapter.getItem(postion);

                //获取司机id
                UserModel currentDriver = (UserModel) SPUtils.readObject(CardTicketVoucherListActivity.this,
                        Constants.USERMODEL);
            }

            @Override
            public void onItemSubViewClick(int choice, int postion) {

            }
        });
    }

    @Override
    protected void setListener() {
        mIVBack.setOnClickListener(this);
        mTVEvent.setOnClickListener(this);
        mCBDelAll.setOnClickListener(this);
        mTVDel.setOnClickListener(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        loadNearbySJListList();
    }

    List<String> delOrder = new ArrayList<>();

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    private void loadNearbySJListList() {

        //        String User_id = userModel.getUser_id();
        UserModel userModel = (UserModel) SPUtils.readObject(mApp, Constants.USERMODEL);
        mEngine.getOrderForward(userModel.getMobile()).
                enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        try {
                            String str = response.body().string();

                            LogsUtil.normal("获取卡片服务" + str);
                            CardTicketJsonModel model = GsonUtil.jsonToCardTicketBean(str);
                            CardTicketModel.ResultBean.LdataBean ldataBean = new CardTicketModel.ResultBean.
                                    LdataBean(R.mipmap.icon_card_ticket_day, model.getResult().getCard0_num() + "");
                            CardTicketModel.ResultBean.LdataBean ldataBean2 = new CardTicketModel.ResultBean.LdataBean(
                                    R.mipmap.icon_card_ticket_night, model.getResult().getCard1_num()
                            );
                            List<CardTicketModel.ResultBean.LdataBean> daLdataBeanList = new ArrayList<>();
                            daLdataBeanList.add(ldataBean);
                            daLdataBeanList.add(ldataBean2);

                            mAdapter.clearAll();
                            mAdapter.addAll(daLdataBeanList);

                        } catch (IOException e) {
                            e.printStackTrace();
                            LogsUtil.error("获取附近司机信息" + e);
                        } catch (JSONException e) {
                            e.printStackTrace();
                            LogsUtil.error("获取附近司机信息" + e);
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                    }
                });
    }

    private String fmtDelOrder() {
        String delOrderStr = "";
        if (delOrder != null) {
            for (int i = 0; i < delOrder.size(); i++) {
                if (i == 0) {
                    delOrderStr += delOrder.get(i);
                } else {
                    delOrderStr += "," + delOrder.get(i);
                }
            }
        }
        return delOrderStr;
    }

}
